/*
 * ShoppingCart.java
 *
 * Created on 2010-feb-24, 17:49:53
 */

package dat215projekt.gui.components;

import dat215projekt.data.IMatDataHandlerExtended;
import java.awt.Dimension;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.List;
import se.chalmers.cse.dat215.project.ShoppingCartListener;
import se.chalmers.cse.dat215.project.ShoppingItem;

/**
 *
 * @author Administratör
 */
public class ShoppingCart extends javax.swing.JPanel implements ShoppingCartListener{
    private static final IMatDataHandlerExtended handler = IMatDataHandlerExtended.getInstance();

    private static final NumberFormat moneyFormat;
    static {
        DecimalFormatSymbols symbols = new DecimalFormatSymbols();
        symbols.setDecimalSeparator(',');
        symbols.setGroupingSeparator(' ');

        moneyFormat = new DecimalFormat("###,##0.00 kr",symbols);
    }


    /** Creates new form ShoppingCart */
    public ShoppingCart() {
        initComponents();

        handler.addShoppingCartListener(this);

        numProducts.setText("0");
        totalPrice.setText("0");

        shoppingCartChanged();

        scrollPane.getVerticalScrollBar().setUnitIncrement(40);
    }

    public void shoppingCartChanged() {

        // Create new layout
        List<ShoppingItem> list = handler.getShoppingCartItems();
        cartDataPanel.removeAll();

        // Create and add panels
        for(int i=0;i<list.size();i++){
            ShoppingCartProduct p = new ShoppingCartProduct(list.get(i),i);
            p.setPreferredSize(new Dimension((int)this.getPreferredSize().getWidth(),64));
            cartDataPanel.add(p);

        }

        cartDataPanel.setPreferredSize(new Dimension(240,64*list.size()));

        // Set total labels
        totalPrice.setText(moneyFormat.format(handler.getShoppingCartTotalCost()));
        numProducts.setText(list.size()+"");

        // För att få allt att funka
        scrollPane.revalidate();
        scrollPane.repaint();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        scrollPane = new javax.swing.JScrollPane();
        cartDataPanel = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        numProducts = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        totalPrice = new javax.swing.JLabel();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(dat215projekt.Dat215projektApp.class).getContext().getResourceMap(ShoppingCart.class);
        setBackground(resourceMap.getColor("Form.background")); // NOI18N
        setMaximumSize(new java.awt.Dimension(251, 431));
        setMinimumSize(new java.awt.Dimension(251, 431));
        setName("Form"); // NOI18N
        setOpaque(false);

        jLabel1.setBackground(resourceMap.getColor("jLabel1.background")); // NOI18N
        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabel1.setName("jLabel1"); // NOI18N
        jLabel1.setOpaque(true);

        scrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setAlignmentX(0.0F);
        scrollPane.setName("scrollPane"); // NOI18N
        scrollPane.setOpaque(false);

        cartDataPanel.setBackground(resourceMap.getColor("cartDataPanel.background")); // NOI18N
        cartDataPanel.setMaximumSize(new java.awt.Dimension(32000, 32000));
        cartDataPanel.setMinimumSize(new java.awt.Dimension(240, 257));
        cartDataPanel.setName("cartDataPanel"); // NOI18N
        cartDataPanel.setOpaque(false);
        cartDataPanel.setPreferredSize(new java.awt.Dimension(240, 240));
        cartDataPanel.setLayout(new javax.swing.BoxLayout(cartDataPanel, javax.swing.BoxLayout.PAGE_AXIS));
        scrollPane.setViewportView(cartDataPanel);

        jPanel1.setBackground(resourceMap.getColor("jPanel1.background")); // NOI18N
        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setName("jPanel1"); // NOI18N

        jLabel2.setFont(resourceMap.getFont("totalPrice.font")); // NOI18N
        jLabel2.setForeground(resourceMap.getColor("jLabel2.foreground")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        numProducts.setFont(resourceMap.getFont("totalPrice.font")); // NOI18N
        numProducts.setForeground(resourceMap.getColor("jLabel2.foreground")); // NOI18N
        numProducts.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        numProducts.setText(resourceMap.getString("numProducts.text")); // NOI18N
        numProducts.setName("numProducts"); // NOI18N

        jLabel4.setFont(resourceMap.getFont("totalPrice.font")); // NOI18N
        jLabel4.setForeground(resourceMap.getColor("jLabel2.foreground")); // NOI18N
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setFont(resourceMap.getFont("totalPrice.font")); // NOI18N
        jLabel5.setForeground(resourceMap.getColor("jLabel2.foreground")); // NOI18N
        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        totalPrice.setFont(resourceMap.getFont("totalPrice.font")); // NOI18N
        totalPrice.setForeground(resourceMap.getColor("jLabel2.foreground")); // NOI18N
        totalPrice.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        totalPrice.setText(resourceMap.getString("totalPrice.text")); // NOI18N
        totalPrice.setName("totalPrice"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(numProducts, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4))
                    .addComponent(totalPrice, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4)
                    .addComponent(numProducts))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(totalPrice))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel cartDataPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel numProducts;
    private javax.swing.JScrollPane scrollPane;
    private javax.swing.JLabel totalPrice;
    // End of variables declaration//GEN-END:variables
}
